<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/11 0011
 * Time: 9:25
 */
namespace app\admin\controller;
use app\admin\model\UpFile;
use think\Db;
use think\Request;

class Api
{
    /**获取第二类分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function get_Branch(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return json(['code'=>200,'data'=>[]]);
        }
        $data = Db::name('branch')->where('tables_class',$id)->select();
        return json(['code'=>200,'data'=>$data]);
    }

    /**导入
     * @param Request $request
     * @return \think\response\Json
     */
    public function daoru(Request $request){
        $data = $request->param();
        $val = val($data,[
            'classes_id|一级分类' =>'require',
            'branch_id|二级分类' =>'require',
            'three_level_id|三级分类'=>'require',
            'type|题目模式'=>'require',
        ]);
        if(!$val['code']){
            return json(['code'=>203,'msg'=>$val['msg']]);
        }
        if(!UpFile::isFile('tabl')){
            return json(['code'=>203,'msg'=>'请添加文件']);
        }
        $file = $request->file('tabl');
        $files = explode('.',$file->getInfo('name'));
        $fileType = $files[1];
        $fileName = $file->getInfo('tmp_name');
        $objPHPExcel  = new \PHPExcel();
        if($fileType=='xls'){
            $objReader  = \PHPExcel_IOFactory::createReader('Excel5');
        }else{
            $objReader  = \PHPExcel_IOFactory::createReader('Excel2007');
        }
        $obj_PHPExcel  = $objReader->load($fileName,$encode = 'utf-8');
        $excel_array = $obj_PHPExcel->getSheet(0)->toArray();
        $chucuo = [];
        foreach ($excel_array as $value){
            $update = [];
            if(!$value[0]||$value[0]=='序号'){
                continue;
            }else{
//                return $value;
                $update['stem'] = $value[1];
                $answer = [];
                $answer1 = explode('；',$value[2]);
                foreach ($answer1 as $values){
                    $answer2 = explode(';',$values);
                    foreach ($answer2 as $valuess){
                        $answer = array_merge($answer,explode('。',$valuess));
                    }
                }
                $update['answer'] = [];
                foreach ($answer as $va){
                    if($va){
                        $update['answer'][] = $va;
                    }
                }
                if($value[3]=='A'){
                    $update['right_key'] = 0;
                }elseif($value[3]=='B'){
                    $update['right_key'] = 1;
                }elseif($value[3]=='C'){
                    $update['right_key'] = 2;
                }elseif($value[3]=='D'){
                    $update['right_key'] = 3;
                }elseif($value[3]=='E'){
                    $update['right_key'] = 4;
                }elseif($value[3]=='F'){
                    $update['right_key'] = 5;
                }elseif($value[3]=='G'){
                    $update['right_key'] = 6;
                }
                $update['thinking'] = $value[4];
                $update['coin']     = $value[5];
                $update['create_time'] = time();
                $update['experience'] = $value[6];
                $update['difficulty'] = $value[7];
                $update['classes_id'] = $data['classes_id'];
                $update['branch_id'] = $data['branch_id'];
                $update['three_level_id'] = $data['three_level_id'];
                $update['status'] =1;
                $update['type']  = $data['type'];
//                return $update;
//                $chucuo[] = $update;
                if(!Db::name('bank')->json(['answer'])->insert($update)){
                    $chucuo[] = $update;
                }
            }
        }
//        return $chucuo;
        if($chucuo){
            return json(['code'=>203,'msg'=>'题目没有全部传完','data'=>$chucuo]);
        }else{
            return json(['code'=>200,'msg'=>'题目上传成功']);
        }
    }

    /**获取第三类分类
     * @param Request $request
     * @return \think\response\Json
     */
    public function getBank(Request $request){
        $id = $request->param('id',null);
        $three_level = Db::name('three_level')->where('branchid',$id)->select();
        return json(['code'=>200,'data'=>$three_level]);
    }

    /**获取三级分类详情
     * @param Request $request
     * @return \think\response\Json
     */
    public function three_level_info(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('three_level')->where('id',$id)->find();
        return json(['code'=>200,'data'=>$data]);
    }

    /**审核是否通过函数
     * @param Request $request
     * @return \think\response\Json
     */
    public function shenghe(Request $request){
        $id = $request->param('id',null);
        if(Db::name('bank')->where('id',$id)->update(['status'=>1])){
            return json(['code'=>200,'msg'=>'审核通过']);
        }else{
            return json(['code'=>203,'msg'=>'审核失败']);
        }
    }

    /**修改用户状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function userStatus(Request $request){
        $id = $request->param('id',null);
        $status = $request->param('status',null);
        if(Db::name('user')->where('id',$id)->update(['status'=>$status])){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败,你没有做出改变']);
        }
    }

    /**处理完成的函数
     * @param Request $request
     * @return \think\response\Json
     */
    public function forward(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('forward')->where('id',$id)->find();
        if($data['status']==1){
            return json(['code'=>200,'msg'=>'你已经处理过这个问题了']);
        }
        if(Db::name('forward')->where('id',$id)->update(['status'=>1])){
            return json(['code'=>200,'msg'=>'处理成功']);
        }else{
            return json(['code'=>203,'msg'=>'处理失败']);
        }
    }

}